package com.harveyh.cemax;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.harveyh.cemax.common.EActivity;
import com.harveyh.cemax.common.GloableData;
import com.harveyh.cemax.config.Preferences;
import com.harveyh.cemax.manager.DBManager;
import com.harveyh.cemax.manager.UtilsManager;

public class StartActivity extends EActivity {
	private LinearLayout ll_shunxu, ll_cuoti;
	private RadioGroup radioGroup;
	private TextView tv_type;
	String[] type;
	/** 0~5分别代表chuji，zhongji，gaoji，jishi，gaojijishi*/
	int whichTable = 0;
	/** 题目类型：xuanze，panduan，quanbu*/
	String timuType = "xuanze";
	Preferences pre;
	String lastReadInfo = "";
	HashMap<Integer, Integer> cache;
	int typeId;
	DBManager db;
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start_activity);
		init();
		db = DBManager.open(this);
		pre = Preferences.getInstance(this);
		lastReadInfo = pre.getLastReadTimuInfo();
//		cache = getLastReadInfo(lastReadInfo);
		type = new String[] { "初级工", "中级工", "高级工", "技师", "高级技师" };
	}

	private void init() {
		ll_shunxu = (LinearLayout) findViewById(R.id.linearLayout_shunxu);
		ll_cuoti = (LinearLayout) findViewById(R.id.linearLayout_cuoti);
		tv_type = (TextView) findViewById(R.id.tv_type);
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

		ll_shunxu.setOnClickListener(this.onClick());
		ll_cuoti.setOnClickListener(this.onClick());
		tv_type.setOnClickListener(this.onClick());
		
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.radio_xuanze:
					timuType = "xuanze";
					break;
				case R.id.radio_panduan:
					timuType = "panduan";
					break;
				case R.id.radio_quanbu:
					timuType = "quanbu";
					break;
				default:
					break;
				}
			}
		});
	}

	private View.OnClickListener onClick() {
		return new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (v == ll_shunxu) {
//					setTypeId(whichTable, false);
					typeId = UtilsManager.getTypeId(whichTable, timuType, false);
					final int localId = db.getDataIdFromLastReadInfo(typeId);
					if (localId <= 0) {
						Intent intent = new Intent(StartActivity.this, DatiActivity.class);
						intent.putExtra("whichTable", whichTable);
						intent.putExtra("type", timuType);
						intent.putExtra("isCuoti", false);
						startActivity(intent);
					} else {
						// preference里的缓存再仔细考虑清楚
						new AlertDialog.Builder(StartActivity.this).setTitle("提示")
							.setMessage("您上次在进行练习的时候中途退出，是否要回到上一次练习的题目？")
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									Intent intent = new Intent(StartActivity.this, DatiActivity.class);
									intent.putExtra("localId", localId);
									intent.putExtra("whichTable", whichTable);
									intent.putExtra("type", timuType);
									intent.putExtra("isCuoti", false);
									startActivity(intent);
								}
							}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int which) {
									Intent intent = new Intent(StartActivity.this, DatiActivity.class);
									intent.putExtra("whichTable", whichTable);
									intent.putExtra("type", timuType);
									intent.putExtra("isCuoti", false);
									startActivity(intent);
								}
							}).show();
					}
				} else if (v == ll_cuoti) {
					typeId = UtilsManager.getTypeId(whichTable, timuType, true);
					final int localId = db.getDataIdFromCuotiLastReadInfo(typeId);
					if (localId <= 0) {
						Intent intent = new Intent(StartActivity.this, DatiActivity.class);
						intent.putExtra("whichTable", whichTable);
						intent.putExtra("type", timuType);
						intent.putExtra("isCuoti", true);
						startActivity(intent);
					} else {
						new AlertDialog.Builder(StartActivity.this).setTitle("提示")
						.setMessage("您上次在进行练习的时候中途退出，是否要回到上一次练习的题目？")
						.setPositiveButton("确定", new DialogInterface.OnClickListener() {
							@Override   
							public void onClick(DialogInterface dialog, int which) {
								Intent intent = new Intent(StartActivity.this, DatiActivity.class);
								intent.putExtra("localId", localId);
								intent.putExtra("whichTable", whichTable);
								intent.putExtra("type", timuType);
								intent.putExtra("isCuoti", true);
								startActivity(intent);
							}
						}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								Intent intent = new Intent(StartActivity.this, DatiActivity.class);
								intent.putExtra("whichTable", whichTable);
								intent.putExtra("type", timuType);
								intent.putExtra("isCuoti", true);
								startActivity(intent);
							}
						}).show();
					}
				} else if (v == tv_type) {
					new AlertDialog.Builder(StartActivity.this)
						.setItems(type,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									tv_type.setText(type[which]);
									whichTable = which;
								}
							}).show();
				}
			}
		};
	}
	
	
	
//	private HashMap<Integer, Integer> getLastReadInfo(String json) {
//		if (TextUtils.isEmpty(json)) {
//			return null;
//		}
//		HashMap<Integer, Integer> map = null;
//		try {
//			JSONArray array = new JSONArray(json);
//			JSONObject obj = null;
//			int length = array.length();
//			map = new HashMap<Integer, Integer>();
//			for (int i = 0; i < length; i++) {
//				obj = array.getJSONObject(i);
//				map.put(obj.getInt("typeId"), obj.getInt("typeId"));
//			}
//			return map;
//		} catch (JSONException e) {
//			e.printStackTrace();
//			return null;
//		}
//	}
}